/*

@requires jQuery/jquery.js
@requires jQuery/plugins/jquery.tablesorter.js


Tablesorter widget to enable selcting rows.

*/
$.tablesorter.addWidget({
    /*
    
    Property: id
    The id of this widget.
    
    */
    id: 'select',
    
    /*
    
    Method: init
    Constructor.
   
    */
    init: function(table){
        table.config.select = $.extend({}, this.defaults, table.config.select);

        var tr = $('tbody tr', table);
        $(tr)
         .unbind('mousedown.tablesorter.select')
         .bind('mousedown.tablesorter.select', {
            table: table
        }, $.proxy(this, 'onClick'));
        
        $(table)
         .unbind('update.tablesorter.select')
         .bind('update.tablesorter.select', $.proxy(this, 'update'));
        
        // Disable textselection for IE
        if($.browser.msie){
			$(tr)
			 .unbind('selectstart.tablesorter.select')
			 .bind('selectstart.tablesorter.select',function(){return false;});
		}
    },
    
    /*
    
    Method: onClick
    Will be triggerd on mouseout event. Triggers event on stored callbacks.
    
    Parameters:
        e - {Event Object} The event object.
    
    */
    onClick: function(e) {
        var table = $(e.data.table)[0];
        var elem = $(e.currentTarget)[0];
        var not = false;
        var tr = $('tbody tr', table);
        
        if(e.which == 1) {     // left mouse button
            if(!e.ctrlKey) {
                $(elem).siblings('.selected').removeClass('selected');
                
                if(e.shiftKey) {
                    not = true;
                    var curId = $(tr).index($(elem));
                    var start, end;
                    
                    if(table.config.select.prevId < curId) {
                        start = table.config.select.prevId;
                        end = curId+1;
                    } else {
                        start = curId;
                        end = table.config.select.prevId +1;
                    }
                        
                    elem = $(tr).slice(start, end);
                }
            }
            $(elem).toggleClass('selected');
        } else {
            if(!$(elem).hasClass('selected')) {
                $(elem).siblings('.selected').removeClass('selected');
                $(elem).addClass('selected');
            }
        }
        
        if(!not)
            table.config.select.prevId = $(tr).index($(elem));
        
        // Trigger tablesorter onselect event.
        $(table).trigger('select.tablesorter.select', [{
            table: $(table), 
            selected: $('tbody tr.selected', table),
            elem: elem
        }]);
        
        // Store slected rows
        table.config.select.selection = $.map($('tbody tr.selected', table), function(elem, i){
            return $($('input', elem)[0]).val();
        });
        
    },
    
    /*
    
    Method: update
    
    */
    update: function(e){
        var table = e.target;
        var preselect = $.map($('tbody tr', table), function(elem, i){
            return $.inArray($($('input', elem)[0]).val(), table.config.select.selection) < 0 ? null : elem;
        });
        $(preselect).addClass('selected');
    },
    
    /**/
    format: function(table){
        this.init(table);
    }
});
